package com.example.demo.web;

import com.example.demo.repository.i.TTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@RequestMapping
@ApiIgnore
@Slf4j
public class Route {
    @Autowired
    TTestService tTestService;

    @GetMapping("/route/{path}")
    public String route(@PathVariable("path") String path) {
        log.info("route:{}", path);
        return path;
    }

    @GetMapping("/")
    public String index() {
        return "home";
    }

    @GetMapping("/oauthTest")
    public String oauthTest() {
        return "component/log";
    }

/*
    @GetMapping(value = "/info")
    public Object index(HttpServletRequest request) {
        ServletContext servletContext = request.getSession().getServletContext();
        if (servletContext == null) {
            return null;
        }
        WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        //请求url和处理方法的映射
        List<RequestToMethodItem> requestToMethodItemList = new ArrayList<RequestToMethodItem>();
        //获取所有的RequestMapping
        Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext,
                HandlerMapping.class, true, false);

        for (HandlerMapping handlerMapping : allRequestMappings.values()) {
            //本项目只需要RequestMappingHandlerMapping中的URL映射
            if (handlerMapping instanceof RequestMappingHandlerMapping) {
                RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;
                Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
                for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet()) {
                    // 获取实体对象
                    RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();
                    HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue();

                    // 获取请求方式GET/POST/PUT/DELETE（如果@RequestMapping中没有写具体的请求方式则获取不到哦）
                    RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition();
                    Iterator<RequestMethod> it = methodCondition.getMethods().iterator();
                    String requestType = it.hasNext() ? it.next().name() : "";
                    // 获取请求的URL
                    PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
                    Iterator<String> iterator = null;
                    if (patternsCondition != null) {
                        iterator = patternsCondition.getPatterns().iterator();
                    }
                    String requestUrl = iterator.hasNext() ? iterator.next() : "";
                    //String requestUrl = SetUtils.first(patternsCondition.getPatterns());

                    //获取接口所在的类名,方法名,参数名
                    String controllerName = mappingInfoValue.getBeanType().toString();
                    String requestMethodName = mappingInfoValue.getMethod().getName();
                    Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();
                    RequestToMethodItem item = new RequestToMethodItem(requestUrl, requestType, controllerName, requestMethodName,
                            methodParamTypes);
                    requestToMethodItemList.add(item);
                }
                break;
            }
        }
        //转化成json打印输出至控制台
        String s = JSON.toJSONString(requestToMethodItemList);
        System.out.println("List:" + s);
        return requestToMethodItemList;
    }

    public static class RequestToMethodItem {
        public String controllerName;
        public String methodName;
        public String requestType;
        public String requestUrl;
        public Class<?>[] methodParmaTypes;

        public RequestToMethodItem(String requestUrl, String requestType, String controllerName, String requestMethodName,

                                   Class<?>[] methodParmaTypes) {
            this.requestUrl = requestUrl;
            this.requestType = requestType;
            this.controllerName = controllerName;
            this.methodName = requestMethodName;
            this.methodParmaTypes = methodParmaTypes;

        }
    }*/
}
